import json

from django.core import serializers
from django.db.models import QuerySet
from django.db import models


class MyJsonEncoder(json.JSONEncoder):# Model实例对象序列化 json.dumps(params, cls=MyJsonEncoder)
    def default(self,obj):
		#serializer 只支持序列化queryset集合对象返回的结果集
		#encodedjson = serializers.serialize('json',guide)
		#decodejson = serializers.deserialize(encodedjson)
        if isinstance(obj,QuerySet):
            return json.loads(serializers.serialize("json",obj))
        if isinstance(obj,models.Model):#处理当个对象
            return json.loads(serializers.serialize('json',[obj])[1:-1])
        if hasattr(obj,'isoformat'): # 处理日期类型
            return obj.isoformat()
        return json.JSONEncoder.default(self,obj)